package com.example.springboot.mqtt.serivice.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.springboot.mqtt.serivice.ILotteryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
public class LotteryServiceImpl implements ILotteryService {

    @Override
    public String getLottery(String res) {
        log.info(res);
        JSONObject lottery = JSONUtil.parseObj(res);
        Object o = lottery.get("words_result");
//        System.out.println(o);
        JSONArray objects = JSONUtil.parseArray(o);
//        System.out.println(objects);
        if(objects.toString().contains("双色球")) {

            StringBuilder con = new StringBuilder();
            for (int i = 0; i < objects.size(); i++) {
                JSONObject jsonObject = JSONUtil.parseObj(objects.get(i));
                Object words = jsonObject.get("words");
                con.append(words);
            }
            log.info(con.toString());

            String str = con.toString();
            int a = str.indexOf("A.");
            int b = str.indexOf("开奖期：");
            String substring = str.substring(a, b);
//            System.out.println(substring);

            String[] split = substring.split("\\)");
            for (String s : split) {
//                System.out.println(s);
                String[] split1 = s.split("\\.");
//                System.out.println(split1[1]);
                String[] split2 = split1[1].split("-");

                String blueStr = split2[0];
//                System.out.println(blueStr);
                log.info("===lottery===");
                StringBuilder bluesb=new StringBuilder();
                for (int blueI = 0; blueI < blueStr.length(); blueI+=2) {
                    String blueBall = blueStr.substring(blueI, Math.min(blueI + 2, blueStr.length()));
                    bluesb.append(blueBall).append(",");
                }


                String redStr = split2[1];
                String redBall = redStr.substring(0, 2);
                bluesb.append(redBall);
                log.info(bluesb.toString());
                log.info("===lottery===");
            }
        }

        return null;
    }

    @Override
    public String getLotteryRegex(String lottery) {
        System.out.println(lottery);
        String regex = "[A-E]\\.(\\d{12})-(\\d{2})\\(\\d\\)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(lottery);

        List<String> doubleColorBallNumbers = new ArrayList<>();

        while (matcher.find()) {
            String redNumbers = matcher.group(1);
            String blueNumber = matcher.group(2);
            String doubleColorBallNumber = redNumbers + "-" + blueNumber;
            doubleColorBallNumbers.add(doubleColorBallNumber);
        }

        // 输出结果
        for (String number : doubleColorBallNumbers) {
            System.out.println(number);
        }
        return null;
    }
}
